package model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.json.JSONObject;

@Entity
public class Checkin implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Column
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer idCheckin;
	
	@JoinColumn(name="idItem", referencedColumnName="idItem")
	@ManyToOne
	private Item item;		
	
	@JoinColumn(name="idMember", referencedColumnName="idMember")
	@ManyToOne
	private Member member;	
	
	@Column(name="datetime")
	@Temporal(TemporalType.TIMESTAMP)
	private Date datetime; 
	
	public Checkin() {
	}
	
	public Checkin(Integer idCheckin) {
		this.idCheckin = idCheckin;
	}
	
	@Override
	public String toString() {
		try{
			JSONObject jsonObj = new JSONObject();
			jsonObj.put("idCheckin", this.getIdCheckin());
			jsonObj.put("datetime", this.getDatetime());
			
			if(this.getItem() != null){
				jsonObj.put("item", new JSONObject(this.getItem().toString()));
			}

			if(this.getItem() != null){
				jsonObj.put("member", new JSONObject(this.getMember().toString()));
			}
			
			return jsonObj.toString();
		}catch (Exception e){
			throw new RuntimeException(e);
		}
	}

	public Integer getIdCheckin() {
		return idCheckin;
	}

	public void setIdCheckin(Integer idCheckin) {
		this.idCheckin = idCheckin;
	}

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public Date getDatetime() {
		return datetime;
	}

	public void setDatetime(Date datetime) {
		this.datetime = datetime;
	}	

}
